﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityModel;
using DataAccess;
using System.Data.Objects;
using System.Data;
using System.Data.SqlClient;

namespace Business
{
    public class BulkOrderService : BusinessBase<BulkOrder, UserModel>
    {
        public List<string> GetAllProductCode()
        {
            return this.retreiveHelper.GetRepositoryWithoutTracking<BulkOrder>().Select(t => t.ProductCode).OrderBy(t => t).ToList();
        }

        public Result<BulkOrder> GetAllBulkOrdersByConsolidation(int consolidateID, Int16 currentPage, Int16 pageSize, string sortCol,
            Common.SORT_ORDER sortOrder, UserModel userInfo)
        {
            var result = new Result<BulkOrder>();

            List<ObjectParameter> parameters = new List<ObjectParameter>();
            parameters.Add(new ObjectParameter("conPointID", SqlDbType.Int) { Value = consolidateID });
            parameters.Add(new ObjectParameter("idsSort", SqlDbType.Int) { Value = -1 });
            parameters.Add(new ObjectParameter("quantity", SqlDbType.Int) { Value = 0 });
            parameters.Add(new ObjectParameter("IsUSUser", SqlDbType.Bit) { Value = 0 });
            parameters.Add(new ObjectParameter("OwnerId", SqlDbType.Int) { Value = -1 });
            parameters.Add(new ObjectParameter("UserID", SqlDbType.Int) { Value = 1000 });
            parameters.Add(new ObjectParameter("territoryId", SqlDbType.Int) { Value = -1 });
            parameters.Add(new ObjectParameter("PageIndex", SqlDbType.Int) { Value = currentPage });
            parameters.Add(new ObjectParameter("PageSize", SqlDbType.Int) { Value = pageSize });
            parameters.Add(new ObjectParameter("SortCol", SqlDbType.VarChar) { Value = sortCol });
            parameters.Add(new ObjectParameter("Sorting", SqlDbType.Bit) { Value = sortOrder });

            var boList = this.executeHelper.ExecuteQuery<spBulkOrdersIDSReport_Result>("spBulkOrdersIDSReport", userInfo, parameters.ToArray());

            parameters = null;

            result.EntityList = boList.EntityList.Select(t => 
                Common.Functions.TrustlyMapping<BulkOrder, spBulkOrdersIDSReport_Result>(t, new BulkOrder())).ToList();
            result.TotalRecords = boList.TotalRecords;

            return result;
        }
    }
}
